jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.chart.serviceRequestsPie.ServiceRequestsPie", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.displayServiceRequestsPieSubject = "com.tibco.admin.monitormanage.uicomponents.chart.serviceRequestsPie.ServiceRequestsPie.displayServiceRequestsPieSubject";
        
        	plugin.prototype.chartId = null;
        	
        	plugin.prototype.isServiceInstance;
        	plugin.prototype.isTimeframe;
        	plugin.prototype.chartLoaded = false;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }

            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(chartId, isTimeframe, isServiceInstance) {
            	this.chartId = chartId;

            	this.isServiceInstance = isServiceInstance;
            	this.isTimeframe = isTimeframe;
            	this.chartLoaded = false;
            	
            	var topic = tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.getCacheInfoSubject(!isTimeframe, isServiceInstance);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(topic, this.handleDisplayScreen, this);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(plugin.displayServiceRequestsPieSubject, this.displayServiceRequestsPie, this);
            }
            
            /**
             *  This method will display the service requests pie chart.  This method should only be invoked if the
             *  hideServiceRequestsPie function was invoked previously to hide the chart.
             *
             *  @param chartIdVal the id of the chart to display.  If null is passed all instances of the
             *         service requests pie chart will be displayed
             */
            plugin.displayServiceRequestsPie = function(chartIdVal) {
            	var message = { displayChart:true };
            	if(chartIdVal)
            	{ message.chartId = chartIdVal; }
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.displayServiceRequestsPieSubject, message);
            }
            
            /**
             *  This method will hide the service requests pie chart.  This comes in handy considering the chart
             *  is a heavy-weight component which will cover pop up dialogs.
             *
             *  Note: if a component hides the chart it is the responsibility of that component to display the chart
             *        again when necessary.
             *
             *  @param chartIdVal the id of the chart to hide.  If null is passed all instances of the
             *         service requests pie chart will be hidden
             */
            plugin.hideServiceRequestsPie = function(chartIdVal) {
            	var message = { hideChart:true };
            	if(chartIdVal)
            	{ message.chartId = chartIdVal; }
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.displayServiceRequestsPieSubject, message);
            }
            
            /**
             *  Not for public consumption.
             */
            plugin.prototype.displayServiceRequestsPie = function(subject, message, serviceRequestsObj) {
            	var displayStyle = jsx3.gui.Block.DISPLAYBLOCK;
            	if(message.hideChart)
            	{ displayStyle = jsx3.gui.Block.DISPLAYNONE; }
            	if(!message.chartId || message.chartId == serviceRequestsObj.chartId)
            	{
	            	var chartPane = serviceRequestsObj.getDescendantOfName("chartPane");
            		chartPane.setDisplay(displayStyle, true);
            	}
            }
            
            plugin.prototype.handleDisplayScreen = function(subject, message, serviceRequestsObj) {
            	serviceRequestsObj.loadChart(serviceRequestsObj);
            }

	    	plugin.prototype.loadChart = function(serviceRequestsObj) {
	    		var cacheId = tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.getCacheInfoId(!serviceRequestsObj.isTimeframe, serviceRequestsObj.isServiceInstance);
				var server = tibco.admin.Main.getServer();
            	var serviceInfoDoc = server.Cache.getDocument(cacheId);
            	
            	// initialize the labels
            	var timeframeLabel = serviceRequestsObj.getDescendantOfName("timeframeLabel");
            	if(serviceRequestsObj.isTimeframe)
                   { timeframeLabel.setText(tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeName(), true); }
            	else {
					timeframeLabel.setText(server.getDynamicProperty("@MM@general@timeFrameSinceStarted"));
				}
            	timeframeLabel.repaint();
				
            	var record = serviceInfoDoc.getFirstChild();
            	
            	var faultsLabel = serviceRequestsObj.getDescendantOfName("faultsValueLabel");
            	faultsLabel.setText(record.getAttribute("faults"));
            	faultsLabel.repaint();
            	var successesLabel = serviceRequestsObj.getDescendantOfName("successesValueLabel");
            	successesLabel.setText(record.getAttribute("successes"));
            	successesLabel.repaint();
            		
            	// transform the xml
            	var transformXSL = tibco.admin.Main.loadResource("tibco/admin/monitormanage/uicomponents/chart/serviceRequestsPie/xsl/serviceRequestsPie.xsl", serviceRequestsObj.getNamespace(), "IGNORED_ID", "xsl");
             	var transformedXML = serviceInfoDoc.transformNode(transformXSL);

// Commenting this conditional out considering the chart update is causing a problem...will be fixed later            	
//	    		if(!serviceRequestsObj.chartLoaded)
//            	{
           			serviceRequestsObj.chartLoaded = true;
             			
           			// Load the pie chart
           			var chartId = serviceRequestsObj.chartId;
		       	   	var width = "200";
           			var height = "150";
	   	   			var swfURI = tibco.admin.Main.resolveURI(serviceRequestsObj.getNamespace(), "tibco/admin/monitormanage/resources/fusionCharts/swf/Pie2D.swf");

           			var chartPane = serviceRequestsObj.getDescendantOfName("chartPane");
           			chartPane.setText('<OBJECT classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\' WIDTH=\'' + width + '\' HEIGHT=\'' + height + '\' id=\'' + chartId + '\'><PARAM NAME=movie VALUE=\'' + swfURI + '\'><PARAM NAME=\'FlashVars\' VALUE=\'&dataXML=' + transformedXML + '&chartWidth=950&chartHeight=250\'><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#F2F1F1><EMBED src=\'' + swfURI + '\' FlashVars=\'&dataXML=' + transformedXML + '\')\' quality=high bgcolor=#F2F1F1 WIDTH=\'' + width + '\' HEIGHT=\'' + height + '\' NAME=\'' + chartId + '\' TYPE=\'application/x-shockwave-flash\' PLUGINSPAGE=\'http://www.macromedia.com/go/getflashplayer\'></EMBED></OBJECT>');
           			chartPane.repaint();
/*	            }
				else
				{ plugin.setFusionChartData(serviceRequestsObj.chartId, transformedXML); }
*/
            }
            
            plugin.setFusionChartData = function(objFlash, strXML) {
            	//This function updates the data of a FusionCharts present on the page
            	//Get a reference to the movie 
            	var FCObject = plugin.getFusionChartObject(objFlash);
            	//Set the data
             	//Set dataURL to null
             	FCObject.SetVariable('_root.dataURL',"");
             	//Set the flag
             	FCObject.SetVariable('_root.isNewData',"1");
             	//Set the actual data
             	FCObject.SetVariable('_root.newData',strXML);
             	//Go to the required frame
             	FCObject.TGotoLabel('/', 'JavaScriptHandler');
          	};

          	plugin.getFusionChartObject = function(objectName) {
             	if (navigator.appName.indexOf ("Microsoft") !=-1)
             	{ return window[objectName]; } 
             	else
               	{ return document[objectName]; }
          };

        });
